package com.samshow.config;

import com.samshow.constant.Constant;
import com.samshow.dao.MyJdbcQuery;
import com.samshow.vo.ResourcePrivilege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/1/13.
 */

public class GatawaySecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    @Autowired
    private MyJdbcQuery jdbcQuery;

    private static Map<String,Collection<ConfigAttribute>> resourceMap = null;//资源 后期可缓存中取
    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        Collection<ConfigAttribute> configAttributes = null;
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        if(resourceMap==null){
            loadResource();
        }
        Optional<String> optional = resourceMap.keySet().stream().filter(uri->{
            RequestMatcher requestMatcher = new AntPathRequestMatcher(uri);
            if (requestMatcher.matches(request)){
                return true;
            }else {
                return false;
            }
        }).findFirst();
        String uri = null;
        if(optional.isPresent()){
           uri = optional.get();
        }
        if(uri!=null){
            configAttributes = resourceMap.get(uri);
        }
        return configAttributes;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return  resourceMap.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
    }

    @Override
    public boolean supports(Class<?> clazz) {
        System.out.print("---"+ clazz.getName());
        return true;
    }

    @PostConstruct
    public void init(){
        loadResource();
    }

    private void loadResource(){
        if(resourceMap==null) resourceMap = new HashMap<String,Collection<ConfigAttribute>>();
        List<ResourcePrivilege> resourcePrivileges =  jdbcQuery.gePrivilegResource();
        for (ResourcePrivilege resourcePrivilege:resourcePrivileges){
            String resource = resourcePrivilege.getResource();
            String privilege = Constant.ROLE_PREFIX + resourcePrivilege.getPrivilege();
            ConfigAttribute configAttribute = new SecurityConfig(privilege);
            if (resourceMap.containsKey(resource)){
                resourceMap.get(resource).add(configAttribute);
            }else{
                Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
                configAttributes.add(configAttribute);
                resourceMap.put(resource,configAttributes);
            }

        }

    }


}
